package main.Q301_400;

import java.util.*;

public class Q341_350 {
    public static void main(String[] args) {
        System.out.println("Question341：扁平化嵌套列表迭代器");
        System.out.println("Question342：4的幂");
        System.out.println("Question343：整数拆分");
        System.out.println("Question344：反转字符串");
        System.out.println("Question345：反转字符串中的元音字母");
        System.out.println("Question346：");
        System.out.println("Question347：前K个高频元素");
        System.out.println("Question348：");
        System.out.println("Question349：两个数组的交集");
        System.out.println("Question350：两个数组的交集Ⅱ");
    }
}

class Question341 implements Iterator<Integer>{
    private List<Integer> list = new ArrayList<>();
    private int index;

//    private void add(List<NestedInteger> nestedList) {
//        for (NestedInteger nestedInteger : nestedList) {
//            if (nestedInteger.isInteger()) {
//                list.add(nestedInteger.getInteger());
//            } else {
//                add(nestedInteger.getList());
//            }
//        }
//    }
//
//    public Question341(List<NestedInteger> nestedList) {
//        add(nestedList);
//    }

    @Override
    public Integer next() {
        return list.get(index++);
    }

    @Override
    public boolean hasNext() {
        return index < list.size();
    }
}

class Question342{
    public static boolean isPowerOfFour(int n) {
        while (n != 0 && n % 4 == 0) {
            n /= 4;
        }
        return n == 1;
    }
}

class Question343{
    public int integerBreak(int n) {
        if (n==2) return 1;//数学理论过于高深
        else if (n==3) return 2;
        int multi=1;
        while (n>4){
            n-=3;
            multi*=3;
        }
        multi*=n;
        return multi;
    }
}

class Question344{
    public void reverseString(char[] s) {
        int i=0,j=s.length-1;
        char temp=' ';
        while (i<j){
            temp=s[i];
            s[i]=s[j];
            s[j]=temp;
            i++;
            j--;
        }
    }
}

class Question345{
    public static String reverseVowels(String s) {
        Set<Character> set=new HashSet<>();
        set.add('A');
        set.add('a');
        set.add('E');
        set.add('e');
        set.add('I');
        set.add('i');
        set.add('O');
        set.add('o');
        set.add('U');
        set.add('u');
        int i=0,j=s.length()-1;
        char[] str=s.toCharArray();
        char temp=' ';
        while (i<j){
            if (set.contains(str[i])&&set.contains(str[j])){//i，j均未元音字母
                temp=str[i];
                str[i]=str[j];
                str[j]=temp;
                i++;
                j--;
            }else if (!set.contains(str[i])&&set.contains(str[j])) i++;//i不为元音字母，j为元音字母
            else if (set.contains(str[i])&&!set.contains(str[j])) j--;//i为元音字母，j不为元音字母
            else {//i，j均不为元音字母
                i++;
                j--;
            }
        }
        return new String(str);
    }
}

class Question347{
    public int[] topKFrequent(int[] nums, int k) {
        int[] result=new int[k];//大根堆排序，弹出前K个
        Map<Integer,Integer> map=new HashMap<>();
        for (int num:nums) map.put(num,map.getOrDefault(num,0)+1);
        Set<Map.Entry<Integer,Integer>> entries=map.entrySet();
        PriorityQueue<int[]> bigHeap=new PriorityQueue<>((o1,o2)->o2[1]-o1[1]);
        for (Map.Entry<Integer,Integer> entry:entries)
            bigHeap.offer(new int[]{entry.getKey(),entry.getValue()});
        for (int i=0;i<k;i++)
            result[i]=bigHeap.poll()[0];
        return result;
    }
}

class Question349{
    public static int[] intersection(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);//将两个数组分别排序
        Arrays.sort(nums2);
        Set<Integer> set=new HashSet<>();
        int i=0,j=0,count=0;
        while (i< nums1.length&&j< nums2.length){//双指针分别指向两个数组
            if (nums1[i]<nums2[j]) i++;
            else if (nums1[i]>nums2[j]) j++;
            else if (nums1[i]==nums2[j]&&!set.contains(nums1[i])) {
                set.add(nums1[i]);
                i++;
                j++;
            }else {
                i++;
                j++;
            }
        }
        int[] result=new int[set.size()];
        int k=0;
        for (Integer integer:set){
            result[k]=integer;
            k++;
        }
        return result;
    }
}

class Question350{
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> list=new ArrayList<>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i=0,j=0;
        while (i< nums1.length&&j< nums2.length){
            if (nums1[i]<nums2[j]) i++;
            else if (nums1[i]>nums2[j]) j++;
            else {
                list.add(nums1[i]);
                i++;
                j++;
            }
        }
        int[] arr=new int[list.size()];
        for (int k=0;k<list.size();k++) arr[k]=list.get(k);
        return arr;
    }
}